# from django.shortcuts import render

# Create your views here.
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from api.serializers import UserSerializer, GroupSerializer, ToutiaoItemSerializer, ClassificationItemSerializer
from api.models import Toutiao_ShopItem, Classification_Shop
from rest_framework.pagination import PageNumberPagination
from rest_framework_extensions.cache.mixins import CacheResponseMixin


# 分页
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'  # GoodsListView/?p=2&page_size=3
    page_query_param = 'page'  # GoodsListView/?page=2
    max_page_size = 100


class UserViewSet(viewsets.ModelViewSet):
    """
    允许用户查看或编辑的API路径
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
    """
    允许组查看或编辑的API路径
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer


class ToutiaoViewSet(CacheResponseMixin, viewsets.ReadOnlyModelViewSet, viewsets.ModelViewSet):
    """
    允许查看或编辑商品库名单
    """
    queryset = Toutiao_ShopItem.objects.all().order_by('-update_time')
    serializer_class = ToutiaoItemSerializer
    pagination_class = StandardResultsSetPagination  # 支持自定义分页功能


class ClassificationViewSet(CacheResponseMixin, viewsets.ReadOnlyModelViewSet, viewsets.ModelViewSet):
    """
    允许查看或编辑类目库
    """
    queryset = Classification_Shop.objects.all().order_by('-update_time')
    serializer_class = ClassificationItemSerializer
    pagination_class = StandardResultsSetPagination  # 支持自定义分页功能
